<?php

require_once 'Category.php';

class News {

    private $_ID;
    private $_categoryID;
    private $_accountID;
    private $_title;
    private $_alias;
    private $_description;
    private $_content;
    private $_img_file;
    private $_date;
    private $_img_status;
    private $_status;
    private $_order;
    private $_focus;
    private $_numView;
    
    private $_category;
    
    
    
    

    public function getCategory() {
        return $this->_category;
    }

    public function setCategory($category) {
        $this->_category = $category;
    }

    public function getID() {
        return $this->_ID;
    }

    public function setID($ID) {
        $this->_ID = $ID;
    }

    public function getAccountID() {
        return $this->_accountID;
    }

    public function setAccountID($AccountID) {
        $this->_accountID = $AccountID;
    }

    public function getUsername() {
        return $this->Username;
    }

    public function setUsername($Username) {
        $this->Username = $Username;
    }

    public function getPerKeyWord() {
        return $this->PerKeyWord;
    }

    public function setPerKeyWord($PerKeyWord) {
        $this->PerKeyWord = $PerKeyWord;
    }

//put your code here

    public function getNews_Id() {
        return $this->_ID;
    }

    public function setNews_Id($News_Id) {
        $this->_ID = $News_Id;
    }

    public function getCategoryID() {
        return $this->_categoryID;
    }

    public function setCategoryID($CatID) {
        $this->_categoryID = $CatID;
    }

    public function getAccID() {
        return $this->_accountID;
    }

    public function setAccID($AccID) {
        $this->_accountID = $AccID;
    }

    public function getTitle() {
        return $this->_title;
    }

    public function setTitle($Title) {
        $this->_title = $Title;
    }

    public function getAlias() {
        return $this->_alias;
    }

    public function setAlias($alias) {
        $this->_alias = $alias;
    }

    public function getDescription() {
        return $this->_description;
    }

    public function setDescription($Description) {
        $this->_description = $Description;
    }

    public function getContent() {
        return $this->_content;
    }

    public function setContent($Content) {
        $this->_content = $Content;
    }

    public function getImg_file() {
        return $this->_img_file;
    }

    public function setImg_file($img_file) {
        $this->_img_file = $img_file;
    }

    public function getDate() {
        return $this->_date;
    }

    public function setDate($Date) {
        $this->_date = $Date;
    }

    public function getImg_status() {
        return $this->_img_status;
    }

    public function setImg_status($img_status) {
        $this->_img_status = $img_status;
    }

    public function getStatus() {
        return $this->_status;
    }

    public function setStatus($Status) {
        $this->_status = $Status;
    }

    public function getOrder() {
        return $this->_order;
    }

    public function setOrder($Order) {
        $this->_order = $Order;
    }

    public function getFocus() {
        return $this->_focus;
    }

    public function setFocus($Focus) {
        $this->_focus = $Focus;
    }

    public function getNum_view() {
        return $this->_numView;
    }

    public function setNum_view($num_view) {
        $this->_numView = $num_view;
    }

    // tải danh sách bài viết thuộc nhóm tin
    public function getListByCategoryID($db = null) {
        if ($db != null) {

            //$db = new Database();
            $sqlQuery = "SELECT
                                n.`News_Id`,
                                n.`CatID`,
				c.`CatName`,
                                c.`alias` as AliasCategory,
                                n.`AccID`,
                                n.`Title`,
                                n.`alias`,
                                n.`Description`,
                                n.`Content`,
                                n.`img_file`,
                                Date_Format(n.`Date`, '%d/%m/%Y') AS `Date`,
                                n.`img_status`,
                                n.`Status`,
                                n.`Order`,
                                n.`Focus`,
                                n.`num_view`
                              FROM `news` AS n
			      JOIN category AS c ON n.`CatID` = c.`Cat_Id`
                              WHERE n.`Status` = 1
                              AND n.`CatID` = :CatID
                              order by n.`Order` desc";
            $parameters = array(':CatID');
            $values = array($this->getCategoryID());
            $types = array(PDO::PARAM_INT);
            $result = $db->executeQuery($sqlQuery, $parameters, $values, $types);

            $rows = array();
            if ($result != null) {
                while ($row = $result->fetch()) {

                    $news = new News();
                    $news->setID($row->News_Id);
                    $news->setAlias($row->alias);
                    $news->setTitle($row->Title);
                    $news->setDescription($row->Description);
                    $news->setContent($row->Content);
                    $news->setAccID($row->AccID);
                    $news->setCategoryID($row->CatID);
                    $news->setImg_file($row->img_file);
                    $news->setDate($row->Date);
                    $news->setImg_status($row->img_status);
                    $news->setStatus($row->Status);
                    $news->setOrder($row->Order);
                    $news->setFocus($row->Focus);
                    $news->setNum_view($row->num_view);

                    // đối tượng category
                    $category = new Category();
                    $category->setID($news->getCategoryID());
                    $categoryObj = $category->getByID($db);
                    $news->setCategory($categoryObj);
                    $rows[] = $news;
                }
                return $rows;
            }
            else {
                return null;
            }
        }
    }

    // tải danh sách bài viết được xem nhiều
    public function getHotList($db = null) {
        if ($db != null) {

            //$db = new Database();
            $sqlQuery = "SELECT
                                n.`News_Id`,
                                n.`CatID`,
				c.`CatName`,
                                c.`alias` AS AliasCategory,
                                n.`AccID`,
                                n.`Title`,
                                n.`alias`,
                                n.`Description`,
                                n.`Content`,
                                n.`img_file`,
                                Date_Format(n.`Date`, '%d/%m/%Y') AS `Date`,
                                n.`img_status`,
                                n.`Status`,
                                n.`Order`,
                                n.`Focus`,
                                n.`num_view`
                              FROM `news` AS n
			      JOIN category AS c ON n.`CatID` = c.`Cat_Id`
                              WHERE n.`Status` = 1
                              ORDER BY n.`num_view` DESC;";
            $parameters = null;
            $values = null;
            $types = null;
            $result = $db->executeQuery($sqlQuery, $parameters, $values, $types);

            $rows = array();
            if ($result != null) {
                while ($row = $result->fetch()) {

                    $news = new News();
                    $news->setID($row->News_Id);
                    $news->setAlias($row->alias);
                    $news->setTitle($row->Title);
                    $news->setDescription($row->Description);
                    $news->setContent($row->Content);
                    $news->setAccID($row->AccID);
                    $news->setCategoryID($row->CatID);
                    $news->setImg_file($row->img_file);
                    $news->setDate($row->Date);
                    $news->setImg_status($row->img_status);
                    $news->setStatus($row->Status);
                    $news->setOrder($row->Order);
                    $news->setFocus($row->Focus);
                    $news->setNum_view($row->num_view);

                    // đối tượng category
                    $category = new Category();
                    $category->setID($news->getCategoryID());
                    $categoryObj = $category->getByID($db);
                    $news->setCategory($categoryObj);
                    $rows[] = $news;
                }
                return $rows;
            }
            else {
                return null;
            }
        }
    }


    // tải danh sách bài viết thuộc nhóm tin
    public function getListBySessionID($db = null) {
        if ($db != null) {

            //$db = new Database();
            $sqlQuery = "SELECT
                                n.`News_Id`,
                                n.`CatID`,
				c.`CatName`,
                                c.`alias` AS AliasCategory,
                                n.`AccID`,
                                n.`Title`,
                                n.`alias`,
                                n.`Description`,
                                n.`Content`,
                                n.`img_file`,
                                Date_Format(n.`Date`, '%d/%m/%Y') AS `Date`,
                                n.`img_status`,
                                n.`Status`,
                                n.`Order`,
                                n.`Focus`,
                                n.`num_view`
                              FROM `news` AS n
			      JOIN category AS c ON n.`CatID` = c.`Cat_Id`
			      JOIN `session` AS s ON c.SesID = s.Ses_Id
                              WHERE n.`Status` = 1
                              AND `Ses_Id` = :Ses_Id
                              ORDER BY n.`Order` DESC";
            $parameters = array(':Ses_Id');
            $values = array($this->getCategory()->getSessionID());
            $types = array(PDO::PARAM_INT);
            $result = $db->executeQuery($sqlQuery, $parameters, $values, $types);

            $rows = array();
            if ($result != null) {
                while ($row = $result->fetch()) {

                    $news = new News();
                    $news->setID($row->News_Id);
                    $news->setAlias($row->alias);
                    $news->setTitle($row->Title);
                    $news->setDescription($row->Description);
                    $news->setContent($row->Content);
                    $news->setAccID($row->AccID);
                    $news->setCategoryID($row->CatID);
                    $news->setImg_file($row->img_file);
                    $news->setDate($row->Date);
                    $news->setImg_status($row->img_status);
                    $news->setStatus($row->Status);
                    $news->setOrder($row->Order);
                    $news->setFocus($row->Focus);
                    $news->setNum_view($row->num_view);

                    // đối tượng category
                    $category = new Category();
                    $category->setID($news->getCategoryID());
                    $categoryObj = $category->getByID($db);
                    $news->setCategory($categoryObj);
                    $rows[] = $news;
                }
                return $rows;
            }
            else {
                return null;
            }
        }
    }

    // hàm lấy tất cả bài viết
    public function getList($db = null) {
        if ($db != null) {

            //$db = new Database();
            $sqlQuery = "SELECT
                                n.`News_Id`,
                                n.`CatID`,
				c.`CatName`,
                                c.`alias` AS AliasCategory,
                                n.`AccID`,
                                n.`Title`,
                                n.`alias`,
                                n.`Description`,
                                n.`Content`,
                                n.`img_file`,
                                Date_Format(n.`Date`, '%d/%m/%Y') AS `Date`,
                                n.`img_status`,
                                n.`Status`,
                                n.`Order`,
                                n.`Focus`,
                                n.`num_view`
                              FROM `news` AS n
			      JOIN category AS c ON n.`CatID` = c.`Cat_Id`
                              WHERE n.`Status` = 1                              
                              ORDER BY n.`Date` DESC;";


//            $parameters = array(':num_view');
//            $values = array($this->getNum_view());
//            $types = array(PDO::PARAM_INT);


            $result = $db->executeQuery($sqlQuery, null, null, null);

            $rows = array();
            if ($result != null) {
                while ($row = $result->fetch()) {

                    $news = new News();
                    $news->setID($row->News_Id);
                    $news->setAlias($row->alias);
                    $news->setTitle($row->Title);
                    $news->setDescription($row->Description);
                    $news->setContent($row->Content);
                    $news->setAccID($row->AccID);
                    $news->setCategoryID($row->CatID);
                    $news->setImg_file($row->img_file);
                    $news->setDate($row->Date);
                    $news->setImg_status($row->img_status);
                    $news->setStatus($row->Status);
                    $news->setOrder($row->Order);
                    $news->setFocus($row->Focus);
                    $news->setNum_view($row->num_view);

                    // đối tượng category
                    $category = new Category();
                    $category->setID($news->getCategoryID());
                    $categoryObj = $category->getByID($db);
                    $news->setCategory($categoryObj);
                    $rows[] = $news;
                }
                return $rows;
            }
            else {
                return null;
            }
        }
    }

    // tải chi tiết bài viết bởi alias
    public function getByAlias($db = null) {
        if ($db != null) {

            //$db = new Database();
            $sqlQuery = "SELECT
                                n.`News_Id`,
                                n.`CatID`,
                                n.`AccID`,
                                n.`Title`,
                                n.`alias`,
                                n.`Description`,
                                n.`Content`,
                                n.`img_file`,
                                Date_Format(n.`Date`, '%d/%m/%Y') AS `Date`,
                                n.`img_status`,
                                n.`Status`,
                                n.`Order`,
                                n.`Focus`,
                                n.`num_view`
                                FROM
                                `news` as n
                                WHERE n.`alias` = :alias AND n.`Status` = 1";

            $parameters = array(':alias');
            $values = array($this->getAlias());
            $types = array(PDO::PARAM_STR);

            $result = $db->executeQuery($sqlQuery, $parameters, $values, $types);

            if ($result != null) {

                $row = $result->fetch();

                $this->setID($row->News_Id);
                $this->setAlias($row->alias);
                $this->setTitle($row->Title);
                $this->setDescription($row->Description);
                $this->setContent($row->Content);
                $this->setAccID($row->AccID);
                $this->setCategoryID($row->CatID);
                $this->setImg_file($row->img_file);
                $this->setDate($row->Date);
                $this->setImg_status($row->img_status);
                $this->setStatus($row->Status);
                $this->setOrder($row->Order);
                $this->setFocus($row->Focus);
                $this->setNum_view($row->num_view);

                // đối tượng category
                $category = new Category();
                $category->setID($this->getCategoryID());
                $categoryObj = $category->getByID($db);
                $this->setCategory($categoryObj);

                return $this;
            }
            else {
                return null;
            }
        }
    }

    // tải chi tiết bài viết bởi alias
    public function getByID($db = null) {
        if ($db != null) {

            //$db = new Database();
            $sqlQuery = "SELECT
                                n.`News_Id`,
                                n.`CatID`,
                                n.`AccID`,
                                n.`Title`,
                                n.`alias`,
                                n.`Description`,
                                n.`Content`,
                                n.`img_file`,
                                Date_Format(n.`Date`, '%d/%m/%Y') AS `Date`,
                                n.`img_status`,
                                n.`Status`,
                                n.`Order`,
                                n.`Focus`,
                                n.`num_view`
                                FROM
                                `news` as n
                                WHERE n.`News_Id` = :News_Id";

            $parameters = array(':News_Id');
            $values = array($this->getID());
            $types = array(PDO::PARAM_INT);

            $result = $db->executeQuery($sqlQuery, $parameters, $values, $types);

            if ($result != null) {

                $row = $result->fetch();

                $this->setID($row->News_Id);
                $this->setAlias($row->alias);
                $this->setTitle($row->Title);
                $this->setDescription($row->Description);
                $this->setContent($row->Content);
                $this->setAccID($row->AccID);
                $this->setCategoryID($row->CatID);
                $this->setImg_file($row->img_file);
                $this->setDate($row->Date);
                $this->setImg_status($row->img_status);
                $this->setStatus($row->Status);
                $this->setOrder($row->Order);
                $this->setFocus($row->Focus);
                $this->setNum_view($row->num_view);

                // đối tượng category
                $category = new Category();
                $category->setID($this->getCategoryID());
                $categoryObj = $category->getByID($db);
                $this->setCategory($categoryObj);

                return $this;
            }
            else {
                return null;
            }
        }
    }

}

?>
